<template>
  <div class="health-app">
    <!-- 主内容区域 -->
    <router-view></router-view>

    <!-- 底部导航 -->
    <div class="bottom-nav">
      <div class="nav-container">
        <div
            v-for="(tab, index) in tabs"
            :key="index"
            class="nav-item"
            @click="switchTab(index, tab.path)"
        >
          <el-icon :class="{'active-tab': activeTab === index}">
            <component :is="tab.icon" />
          </el-icon>
          <p :class="{'active-tab': activeTab === index}">{{ tab.label }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import {
  HomeFilled,
  Bell,
  User
} from '@element-plus/icons-vue';

const router = useRouter();
const route = useRoute();
const activeTab = ref(0);

// 底部导航
const tabs = [
  { name: 'home', label: '首页', icon: HomeFilled, path: '/' },
  { name: 'orders', label: '订单', icon: Bell, path: '/orders' },
  { name: 'profile', label: '我的', icon: User, path: '/profile' },
];

// 根据当前路由更新activeTab
const updateActiveTab = () => {
  const currentPath = route.path;
  const index = tabs.findIndex(tab => tab.path === currentPath);
  if (index !== -1) {
    activeTab.value = index;
  }
};

// 初始化时执行一次
updateActiveTab();

// 监听路由变化
watch(() => route.path, () => {
  updateActiveTab();
});

const switchTab = (index, path) => {
  activeTab.value = index;
  router.push(path);
};
</script>

<style scoped>
/* 底部导航样式 */
.health-app {
  max-width: 800px;
  margin: 0 auto;
  padding-bottom: 60px;
  min-height: 100vh;
  box-sizing: border-box;
  position: relative;
}

.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: white;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
  z-index: 100;
}

.nav-container {
  max-width: 800px;
  margin: 0 auto;
  height: 60px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.nav-item {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  flex: 1;
}

.nav-item .el-icon {
  font-size: 22px;
  color: #999;
  margin-bottom: 4px;
  transition: all 0.3s ease;
}

.nav-item p {
  font-size: 12px;
  color: #999;
  margin: 0;
  transition: all 0.3s ease;
}

.active-tab {
  color: #00BBFF !important;
}
</style>